package com.lwj.liwanjia.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.google.android.flexbox.FlexboxLayoutManager;
import com.lwj.liwanjia.R;
import com.lwj.liwanjia.config.OnItemClickListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/9/17.
 */

public class TypeAdapter extends RecyclerView.Adapter<TypeAdapter.ViewHolder> {
    private List<String> list;
    private Context context;
    private LayoutInflater inflater;
    private int checkPosition;
    private OnItemClickListener listener;

    public void setListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public void setCheckPosition(int checkPosition) {
        this.checkPosition = checkPosition;
    }

    public TypeAdapter(List<String> list, Context context) {
        this.list = list;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.type_item, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        holder.tvType.setText(list.get(position));
        if (position == checkPosition) {
            holder.tvType.setBackgroundResource(R.drawable.pingjia_shape1);
            holder.tvType.setTextColor(context.getResources().getColor(R.color.colorWite));
        } else {
            holder.tvType.setBackgroundResource(R.drawable.pingjia_shape);
            holder.tvType.setTextColor(context.getResources().getColor(R.color.colorEC921A));
        }
        if (listener != null) {
            holder.tvType.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.OnItemClick(v, position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_type)
        TextView tvType;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
